package com.trackerapp.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;


public class MessageUtil {
    private static final String TAG = MessageUtil.class.getSimpleName();
    private static Context context;
    private static final boolean LOG_ENABLED = false;


//  ############################################################################
//  #                                MessageUtil                               #
//  ############################################################################
    // Mandatory: We need to initialize this class when the application starts.
    public static void initializeMesageUtil(Context applicationContext) {
        context = applicationContext;
    }


    public static void showMessage(String message, boolean displayedByLongTime) {
        if (displayedByLongTime) {
            Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    private static String currentDateTime() {
        SimpleDateFormat sFmt = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z");
        return sFmt.format(new java.util.Date());
    }

    public static void logMessage(String message, boolean displayByLongTime) {
    	if (!LOG_ENABLED) return;
    	
        File root = null;
        File logfile = null;
        FileWriter writer = null;
        boolean externalStorageAvailable = false;
        boolean externalStorageWriteable = false;
        try {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                externalStorageAvailable = externalStorageWriteable = true;
            } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
                externalStorageAvailable = true;
                externalStorageWriteable = false;
            } else {
                externalStorageAvailable = externalStorageWriteable = false;
            }
            System.out.println( " ####### externalStorageAvailable::" + externalStorageAvailable + ":externalStorageWriteable:" + externalStorageWriteable);
            if (externalStorageAvailable && externalStorageWriteable) {
                root = new File(Environment.getExternalStorageDirectory(), "DivaLogs");
                if (!root.exists()) {
                    root.mkdirs();
                }
                logfile = new File(root, "Location.log");
                writer = new FileWriter(logfile, true);
                StringBuffer buf = new StringBuffer();
                buf.append(currentDateTime());
                buf.append(' ');
                buf.append(message);
                buf.append('\n');
                writer.append(buf.toString());
                writer.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//  ############################################################################
}
